﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Sinacor.Infra.Tools.LanguageImporter.Console
{
    class Program
    {
        static StringBuilder _eventLog = new StringBuilder();
        static bool _error = false;

        static int Main(string[] args)
        {
            string path, sheetName, language = null;

            ShowInformation("Sinacor Language Importer BM&FBovespa 2010\n");


            if (args.Length == 3 && 
                !string.IsNullOrEmpty(args[0]) && 
                !string.IsNullOrEmpty(args[1]) && 
                !string.IsNullOrEmpty(args[2]))
            {
                path = args[0];
                sheetName = args[1];
                language = args[2];
            }
            else
            {
                ShowInformation(@"Usage: LanguageImporter.exe [ExcelPath] [SheetName] [Language]");
                ShowInformation(@"ExcelPath: Path of Excel file to import");
                ShowInformation(@"SheetName: Name of the sheet to import");
                ShowInformation(@"Language: pt-BR, en-US or es-ES");
                return -1;
            }


            ExcelImporter excelImporter = ExcelImporter.GetInstance();
            excelImporter.Status += new EventHandler<StatusEventArgs>(excelImporter_Status);

            try
            {
                excelImporter.Import(path, sheetName, language);
            }
            catch (Exception ex)
            {
                ShowError(ex);
                return -1;
            }
            finally
            {
                EventLog.WriteEntry("Sinacor Language Importer", _eventLog.ToString(), !_error ? EventLogEntryType.Information : EventLogEntryType.Error);
            }

            return 0;
        }

        static void excelImporter_Status(object sender, StatusEventArgs e)
        {
            switch (e.Type)
            {
                case StatusType.Information:
                    ShowInformation(e.Message);
                    break;
                case StatusType.Step:
                    ShowStep(e.Message);
                    break;
                case StatusType.Success:
                    ShowSuccess(e.Message);
                    break;
                case StatusType.Error:
                    if (e.Error != null)
                        ShowError(e.Error);
                    else
                        ShowError(e.Message);
                    break;
                default:
                    ShowInformation(e.Message);
                    break;
            }
        }

        private static void ShowInformation(string message, params object[] args)
        {
            System.Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));
        }
        private static void ShowStep(string message, params object[] args)
        {
            System.Console.ForegroundColor = ConsoleColor.Cyan;

            System.Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));

            System.Console.ForegroundColor = ConsoleColor.Gray;
        }
        private static void ShowSuccess(string message, params object[] args)
        {
            System.Console.ForegroundColor = ConsoleColor.Green;

            System.Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));

            System.Console.ForegroundColor = ConsoleColor.Gray;
        }
        private static void ShowError(Exception ex)
        {
            ShowError(ex.ToString());
        }
        private static void ShowError(string message)
        {
            _error = true;

            System.Console.ForegroundColor = ConsoleColor.Red;

            System.Console.WriteLine(message);
            System.Console.WriteLine();

            _eventLog.AppendLine(message);
            _eventLog.AppendLine();

            System.Console.ForegroundColor = ConsoleColor.Gray;
        }
    }
}
